﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HotelManagement.PL.FunctionControl;
using HotelManagement.Common;
using HotelManagement.BL;

namespace HotelManagement.PL
{
    public partial class BaseForm : Form
	{
		#region Varible and Properties
		private TradeMngUserControl m_TradeMngUserControl = new TradeMngUserControl();
		private TradeSrchUserControl m_TradeSrchUserControl = new TradeSrchUserControl();
		private GoodsMngUserControl m_GoodMngUserControl = new GoodsMngUserControl();
		private GoodsWarnUserControl m_GoodWarnUserControl = new GoodsWarnUserControl();
		private GoodsEnoughUserControl m_GoodEnoughUserControl = new GoodsEnoughUserControl();
		private UserMngUserControl m_UserMngUserControl = new UserMngUserControl();
		private StartUserControl m_StartUserControl = new StartUserControl();
		#endregion

		#region Constructor
		public BaseForm()
        {
            InitializeComponent();
		}
		#endregion

		#region Event Handler
		/// <summary>
		/// form's load event handler
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void BaseForm_Load(object sender, EventArgs e)
		{
			Init();
		}
		#endregion

		#region Public Method
		/// <summary>
		/// switch user control with user operation.
		/// </summary>
		/// <param name="opType"></param>
		public void SwitchUserControl(OperationType opType)
		{
			ClearUserControl();
			switch (opType)
			{ 
				case OperationType.None:
					m_StartUserControl.Visible = true;
					break;
				case OperationType.TradeMng:
					m_TradeMngUserControl.Visible = true;
					break;
				case OperationType.TradeSrch:
					m_TradeSrchUserControl.Visible = true;
					break;
				case OperationType.GoodsMng:
					m_GoodMngUserControl.Visible = true;
					break;
				case OperationType.UserMng:
					m_UserMngUserControl.Visible = true;
					break;
				default:
					m_StartUserControl.Visible = true;
					break;
			}			
		}
		#endregion

		#region Private Method
		/// <summary>
		/// clear user control's content
		/// </summary>
		private void ClearUserControl()
		{
			m_TradeMngUserControl.Clear();
			m_TradeSrchUserControl.Clear();
			m_GoodMngUserControl.Clear();
			m_StartUserControl.Visible = false;
			m_TradeMngUserControl.Visible = false;
			m_TradeSrchUserControl.Visible = false;
			m_GoodMngUserControl.Visible = false;
			m_UserMngUserControl.Visible = false;
		}

		/// <summary>
		/// update goods's status
		/// </summary>
		private void UpdateGoodsStatus()
		{			
			try
			{
				Goods goods = new Goods();
				GoodsBL searchBL = new GoodsBL();
				searchBL.SearchWarnGoods(ref goods);

				if (goods.GoodsCollection.Rows.Count == 0)
				{
					m_GoodWarnUserControl.SetDataSource(goods.GoodsCollection.Clone());
					m_GoodWarnUserControl.Visible = false;
					m_GoodEnoughUserControl.Visible = true;
				}
				else
				{
					m_GoodWarnUserControl.SetDataSource(goods.GoodsCollection);
					m_GoodEnoughUserControl.Visible = false;
					m_GoodWarnUserControl.Visible = true;					
				}
			}
			catch
			{

			}
		}

		/// <summary>
		/// add user control to groupbox
		/// </summary>
		private void Init()
		{
			//add user control to gbUserOperation
			if (gbUserOperation.Controls != null && gbUserOperation.Controls.Count > 0)
			{
				gbUserOperation.Controls.Clear();
			}
			gbUserOperation.Controls.Add(m_StartUserControl);
			gbUserOperation.Controls.Add(m_TradeMngUserControl);
			gbUserOperation.Controls.Add(m_TradeSrchUserControl);
			gbUserOperation.Controls.Add(m_GoodMngUserControl);
			gbUserOperation.Controls.Add(m_UserMngUserControl);			
			m_TradeMngUserControl.UpdateGoodsEventHandler += new BaseUserControl.UpdateGoodsEvent(UpdateGoodsStatus);
			m_GoodMngUserControl.UpdateGoodsEventHandler += new BaseUserControl.UpdateGoodsEvent(UpdateGoodsStatus);
			//add user control to gbGoodsWarn
			if (gbGoodsStatus.Controls != null && gbGoodsStatus.Controls.Count > 0)
			{
				gbGoodsStatus.Controls.Clear();
			}
			gbGoodsStatus.Controls.Add(m_GoodEnoughUserControl);
			gbGoodsStatus.Controls.Add(m_GoodWarnUserControl);

			UpdateGoodsStatus();
		}
		#endregion
	}
}
